<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;

/**
 * 后台内容控制器
 * @author huajie <banhuajie@163.com>
 */
class SickController extends AdminController {

    public function index()
    {
        $m_sick = M('classify');
        $treatment=M('treatment');
        $tree = $m_sick
              ->field('id, pid, name')
              ->where(array(
                  'pid' => 0
              ))
              ->select();
        foreach ($tree as &$v) {
            $v['_'] = $m_sick
                    ->field('id, pid, name')
                    ->where(array(
                        'pid' => $v['id']
                    ))
                    ->select();
            foreach ($v['_'] as &$vv) {
                $vv['_'] = $m_sick
                         ->field('id, pid, name')
                         ->where(array(
                             'pid' => $vv['id']
                         ))
                         ->select();
                foreach ($vv['_'] as &$vvv) {
                    $vvv['_'] = $treatment
                               ->field('id,name')
                              ->where('diseaseid='.$vvv['id'])
                              ->select();
                }

            }

        }
        C('_SYS_GET_CATEGORY_TREE_', true); //标记系统获取分类树模板
        // dump($tree);die;
        $this->assign('tree', $tree);
        $this->display();
    }

    /**
     * 显示分类树，仅支持内部调
     * @param  array $tree 分类树
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function tree($tree = null){
        C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();
        $this->assign('tree', $tree);
        // dump($tree);die;
        $this->display('tree');
    }

    /**
     * 添加
     */
    public function add($pid = 0)
    {
        $this->assign('pid', $pid);
        $this->display();
    }

    public function doadd()
    {
        $m_classify = M('classify');
        $m_classify->create();
        $m_classify->add();
        $this->redirect('index');
    }

    public function edit($id =0)
    {
        if (IS_GET) {
            $m_classify = M('classify');
            $info = $m_classify->getById($id);
            $this->assign('info', $info);
            $this->display();
        } elseif (IS_AJAX) {
            $m_classify = M('classify');
            $m_classify->create();
            $m_classify->save();
            $info = array(
                'status' => 1,
                'info' => '修改成功',
                'url' => U('index')
            );
            $this->ajaxReturn($info);
        }
    }

    public function doedit()
    {
        $this->redirect('index');
    }

    public function remove($id = 0)
    {
        $m_classify = M('classify');
        $m_classify->where('id = '.$id)->delete();
        $info = array(
            'status' => 1,
            'info' => '删除成功',
            'url' => U('index')
        );
        $this->ajaxReturn($info);
    }

    public function addFangan($id = 0){
        if (IS_GET && $id > 0) {
            $this->assign('pid', $id);
            $this->display();
        } elseif (IS_POST) {
            $m_classify = M('treatment');
            $m_classify->create();
            $m_classify->add();
            $info = array(
                'status' => 1,
                'info' => '修改成功',
                'url' => U('index')
            );
            $this->ajaxReturn($info);
        }
    }

    public function editFangan($id = 0) {
        if (IS_GET) {
            $info = M('treatment')->getById($id);
            $this->assign('info', $info);
            $this->display();
        } elseif (IS_POST) {
            $m_classify = M('treatment');
            $m_classify->create();
            $m_classify->save();
            $info = array(
                'status' => 1,
                'info' => '修改成功',
                'url' => U('index')
            );
            $this->ajaxReturn($info);
        }
    }

    public function delFangan($id = 0) {
        M('treatment')->delete($id);
        $info = array(
            'status' => 1,
            'info' => '修改成功',
            'url' => U('index')
        );
        $this->ajaxReturn($info);
    }

}